package com.jweb.framework.core.support;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.util.Assert;


public class PatternMatcher<S> {
	
	public PatternMatcher(Map map)
    {
        a = new HashMap();
        b = new ArrayList();
        a = map;
        b = new ArrayList(map.keySet());
        Collections.sort(b, new Comparator() {

            public int a(String s, String s1)
            {
                String s2 = s;
                String s3 = s1;
                return s3.compareTo(s2);
            }

            public int compare(Object obj, Object obj1)
            {
                return a((String)obj, (String)obj1);
            }

            final PatternMatcher a;

            
            {
                a = PatternMatcher.this;
            }
        }
);
    }

    public static boolean match(String s, String s1)
    {
        char ac[] = s.toCharArray();
        char ac1[] = s1.toCharArray();
        int i = 0;
        int j = ac.length - 1;
        int k = 0;
        int l = ac1.length - 1;
        boolean flag = s.contains("*");
        if(!flag)
        {
            if(j != l)
                return false;
            for(int i1 = 0; i1 <= j; i1++)
            {
                char c = ac[i1];
                if(c != '?' && c != ac1[i1])
                    return false;
            }

            return true;
        }
        if(j == 0)
            return true;
        char c1;
        for(; (c1 = ac[i]) != '*' && k <= l; k++)
        {
            if(c1 != '?' && c1 != ac1[k])
                return false;
            i++;
        }

        if(k > l)
        {
            for(int j1 = i; j1 <= j; j1++)
                if(ac[j1] != '*')
                    return false;

            return true;
        }
        char c2;
        for(; (c2 = ac[j]) != '*' && k <= l; l--)
        {
            if(c2 != '?' && c2 != ac1[l])
                return false;
            j--;
        }

        if(k > l)
        {
            for(int k1 = i; k1 <= j; k1++)
                if(ac[k1] != '*')
                    return false;

            return true;
        }
        while(i != j && k <= l) 
        {
            int l1 = -1;
            for(int j2 = i + 1; j2 <= j; j2++)
            {
                if(ac[j2] != '*')
                    continue;
                l1 = j2;
                break;
            }

            if(l1 == i + 1)
            {
                i++;
            } else
            {
                int k2 = l1 - i - 1;
                int l2 = (l - k) + 1;
                int i3 = -1;
label0:
                for(int j3 = 0; j3 <= l2 - k2; j3++)
                {
                    for(int k3 = 0; k3 < k2; k3++)
                    {
                        char c3 = ac[i + k3 + 1];
                        if(c3 != '?' && c3 != ac1[k + j3 + k3])
                            continue label0;
                    }

                    i3 = k + j3;
                    break;
                }

                if(i3 == -1)
                    return false;
                i = l1;
                k = i3 + k2;
            }
        }
        for(int i2 = i; i2 <= j; i2++)
            if(ac[i2] != '*')
                return false;

        return true;
    }

    public Object match(String s)
    {
        Object obj = null;
        Assert.notNull(s, "A non-null key must be provided to match against.");
        for(Iterator iterator = b.iterator(); iterator.hasNext();)
        {
            String s1 = (String)iterator.next();
            if(match(s1, s))
            {
                obj = a.get(s1);
                break;
            }
        }

        if(obj == null)
            throw new IllegalStateException((new StringBuilder("Could not find a matching pattern for key=[")).append(s).append("]").toString());
        else
            return obj;
    }

    private Map a;
    private List b;
	
}
